﻿Public Class Request
    Dim _sender As String
    Dim _GUID As String
    Dim _prod As String
    Dim _rel As String
    Dim _ref As String
    Dim _desc As String
    Dim _srcfiles As String()()
    Dim _cmpnts As String()()
    Dim _SQLs As String()
    Dim _impact As String
    Dim _info As String

    Property Sender() As String
        Get
            Return _sender
        End Get
        Set(ByVal value As String)
            _sender = value
        End Set
    End Property
    Property GUID() As String
        Get
            Return _GUID
        End Get
        Set(ByVal value As String)
            _GUID = value
        End Set
    End Property
    Property Product() As String
        Get
            Return _prod
        End Get
        Set(ByVal value As String)
            _prod = value
        End Set
    End Property
    Property Release() As String
        Get
            Return _rel
        End Get
        Set(ByVal value As String)
            _rel = value
        End Set
    End Property
    Property Reference() As String
        Get
            Return _ref
        End Get
        Set(ByVal value As String)
            _ref = value
        End Set
    End Property
    Property Description() As String
        Get
            Return _desc
        End Get
        Set(ByVal value As String)
            _desc = value
        End Set
    End Property
    Property Sourcefiles() As String()()
        Get
            Return _srcfiles
        End Get
        Set(ByVal value As String()())
            _srcfiles = value
        End Set
    End Property
    Property Components() As String()()
        Get
            Return _cmpnts
        End Get
        Set(ByVal value As String()())
            _cmpnts = value
        End Set
    End Property
    Property SQL() As String()
        Get
            Return _SQLs
        End Get
        Set(ByVal value As String())
            _SQLs = value
        End Set
    End Property
    Property Impact() As String
        Get
            Return _impact
        End Get
        Set(ByVal value As String)
            _impact = value
        End Set
    End Property
    Property Info() As String
        Get
            Return _info
        End Get
        Set(ByVal value As String)
            _info = value
        End Set
    End Property
End Class

Public Class BuildCodex
    Inherits Request
    Private Function encode_subfiles(ByVal text As String()) As String
        Dim line As String = ""
        For i = 0 To text.Length - 1
            line &= text(i) & "##"
        Next
        Return line
    End Function   ' Third level data join "##"
    Private Function encode_file(ByVal line As String()) As String
        Dim section As String = ""
        For i = 0 To line.Length - 1
            section &= line(i) & "$$"
        Next
        Return section
    End Function       ' Second level data join "$$"
    Private Function encode_build(ByVal section As String()) As String
        Dim build As String = ""
        For i = 0 To section.Length - 1
            build &= section(i) & ","
        Next
        Return build
    End Function      ' First level data join ","
    Public Function Encode_Request(ByVal req As Request) As String
        Dim email As String = ""
        Dim section As String() = Nothing

        section(0) = req.Sender
        section(1) = req.GUID
        section(2) = req.Product
        section(3) = req.Release
        section(4) = req.Reference
        section(5) = req.Description
        Dim sourcefile As String() = Nothing
        For i = 0 To req.Sourcefiles.Length - 1
            sourcefile(i) = encode_subfiles(req.Sourcefiles(i))
        Next
        section(6) = encode_file(sourcefile)
        Dim component As String() = Nothing
        For i = 0 To req.Components.Length - 1
            component(i) = encode_subfiles(req.Components(i))
        Next
        section(7) = encode_file(component)
        section(8) = encode_file(req.SQL)
        section(9) = req.Impact
        section(10) = req.Info
        email = encode_build(section)
        Return email
    End Function     ' Encode Request variable to request email

    Private Function decode_build(ByVal csv As String) As String()
        Dim param As String() = csv.Split(",")
        Dim build As String() = Nothing
        Dim j As Integer = 0
        For i = 0 To param.Length - 1
            If param(i) = "" Or param(i) = vbCr Or param(i) = vbCrLf Then
                ' Blank space is irrelevant
            Else
                build(j) = param(i)
                j += 1
            End If
        Next
        Return build
    End Function      ' First level data split ","
    Private Function decode_files(ByVal csv As String) As String()
        Dim param As String() = csv.Split("$$")
        Dim files As String() = Nothing
        Dim j As Integer = 0
        For i = 0 To param.Length - 1
            If param(i) = "" Or param(i) = vbCr Or param(i) = vbCrLf Then
                ' Blank space is irrelevant
            Else
                files(j) = param(i)
                j += 1
            End If
        Next
        Return files
    End Function      ' Second level data split "$$"
    Private Function decode_subfiles(ByVal csv As String) As String()
        Dim param As String() = csv.Split("##")
        Dim subs As String() = Nothing
        Dim j As Integer = 0
        For i = 0 To param.Length - 1
            If param(i) = "" Or param(i) = vbCr Or param(i) = vbCrLf Then
                ' Blank space is irrelevant
            Else
                subs(j) = param(i)
                j += 1
            End If
        Next
        Return subs
    End Function   ' Third level data split "##"
    Public Function Decode_Request(ByVal body As String) As Request
        Dim req As Request = Nothing
        Dim test As String() = decode_build(body)

        req.Sender = test(0)
        req.GUID = test(1)
        req.Product = test(2)
        req.Release = test(3)
        req.Reference = test(4)
        req.Description = test(5)
        Dim srcfiles As String() = decode_files(test(6))
        For i = 0 To srcfiles.Length - 1
            req.Sourcefiles(i) = decode_subfiles(srcfiles(i))
        Next
        Dim components As String() = decode_files(test(7))
        For i = 0 To components.Length - 1
            req.Components(i) = decode_subfiles(components(i))
        Next
        req.SQL = decode_files(test(8))
        req.Info = test(9)
        Return req
    End Function     ' Decode request email to Request variable

    Private Function decode_tasklines(ByVal task As String) As String()
        Dim param As String() = task.Split(vbCrLf)
        Dim build As String() = Nothing
        Dim j As Integer = 0

        For i = 0 To param.Length - 1
            If param(i) = "" Or param(i) = " " Then
                ' Blank space is irrelevant
            Else
                build(j) = param(i)
                j += 1
            End If
        Next
        Return build
    End Function  ' First level of data split "vbCrLf"
    Private Function decode_subtext(ByVal line As String) As String()
        Dim text As String() = line.Split(vbTab)
        Return text
    End Function    ' Second level of data split "vbTab"
    Public Function Decode_Task(ByVal request As String) As Request
        Dim req As Request = Nothing
        Dim iSender As Integer = 0
        Dim iGUID As Integer = 0
        Dim iProduct As Integer = 0
        Dim iRelease As Integer = 0
        Dim iReference As Integer = 0
        Dim iDescription As Integer = 0
        Dim iSourcefiles As Integer = 0
        Dim iComponents As Integer = 0
        Dim iSQL As Integer = 0
        Dim iImpact As Integer = 0
        Dim iInfo As Integer = 0
        Dim pos As Integer = 0

        Dim test As String() = decode_tasklines(request)
        For i = 0 To test.Length - 1                        ' Where are the parts
            If test(i).Contains("Sender:") = True Then
                iSender = i
            End If
            If test(i).Contains("ID:") = True Then
                iGUID = i
            End If
            If test(i).Contains("Product:") = True Then
                iProduct = i
            End If
            If test(i).Contains("Release:") = True Then
                iRelease = i
            End If
            If test(i).Contains("Reference:") = True Then
                iReference = i
            End If
            If test(i).Contains("Description:") = True Then
                iDescription = i
            End If
            If test(i).Contains("Sourcefiles:") = True Then
                iSourcefiles = i
            End If
            If test(i).Contains("Components:") = True Then
                iComponents = i
            End If
            If test(i).Contains("SQLs:") = True Then
                iSQL = i
            End If
            If test(i).Contains("Impact Analysis:") = True Then
                iImpact = i
            End If
            If test(i).Contains("Additional Information:") = True Then
                iInfo = i
            End If
        Next

        req.Sender = test(iSender).Substring(8)             ' Grabs Sender email address
        req.GUID = test(iGUID).Substring(4)                 ' Grabs GUID
        req.Product = test(iProduct).Substring(9)           ' Grabs Product
        req.Release = test(iRelease).Substring(9)           ' Grabs Release
        req.Reference = test(iReference).Substring(11)      ' Grabs Reference
        req.Description = test(iDescription).Substring(13)  ' Grabs Description
        Dim source As String = ""
        For i = iSourcefiles To iComponents                 ' Limit lines checked for source files
            Dim file As String() = decode_subtext(test(i))  ' Split lines to get file and revision
            For j = 0 To file.Length - 1
                req.Sourcefiles(i)(j) = file(j)             ' Grabs file, revision for each item in list
            Next
        Next
        source = ""
        For i = iComponents To iSQL                         ' Limit lines checked for components
            Dim file As String() = decode_subtext(test(i))  ' Split lines to get file, type and build
            For j = 0 To file.Length - 1
                req.Components(i)(j) = file(j)              ' Grabs file, revision for each item in list
            Next
        Next
        source = ""
        For i = iSQL To iImpact                             ' Limit lines checked for components
            req.SQL(pos) = test(i).Replace(vbTab, "")       ' Grab SQLs
        Next
        req.Impact = test(iImpact)                          ' Grab Impact Analysis
        req.Info = test(iInfo)                              ' Grab Additional Information
        Return req
    End Function        ' Decode task body to Request variable
End Class
